/*
    api:"添加发布者"
    name:"陈小洋"
    time：2021.1.24
* */
const Publisher = require('../../../control/Publisher/Publisher')
const tools = require('../../../tools/public');
/**
 * @api {post} /publisher/addPublisher 1.添加发布者信息
 * @apiGroup Publisher
 *
 * @apiParam {Number} UserId 用户id(有做过滤无效的用户id)
 * @apiParam {String} CompanyId 公司id(有做过滤无效的公司id，有过滤重复加入公司)
 *
 *
 *
 * @apiParamExample {json} Request-Example
 *{
 *
 *}
 * @apiSuccessExample  {json} Response-Example
 * {
 *   "code": 200,
 *   "msg": "成为发布者成功！"
 *   }
 */
module.exports = async function (req, res, next) {
    let {UserId, CompanyId} = req.body,
        result = null;
    try {
        if (!UserId || !CompanyId) return res.send(tools.ReturnData(204, '参数错误,请认真阅读接口文档！'));

        result = await Publisher.isValid_userId(UserId)
        if(!result.boolean) return res.send(tools.ReturnData(204, '该用户id无效！'));

        result = await Publisher.isValid_companyId(CompanyId)
        if(!result.boolean) return res.send(tools.ReturnData(204, '该公司id无效！'));

        result = await Publisher.isRepeatRegister(UserId,CompanyId)
        if(result.boolean) return res.send(tools.ReturnData(204, '已经加入过该公司了！'));

        result = await Publisher.Add({CompanyId,UserId})
        res.send(tools.ReturnData(200, '成为发布者成功！', result));
    } catch (e) {
        console.log(e)
        return res.send(tools.ReturnData(500, '系统内部错误！'));
    }
};